package example2;

import java.util.LinkedList;
import java.util.List;

/**
 * <h3>designPattern</h3>
 * <p>测试类</p>
 * @author : zhang.bw
 * @date : 2021-02-25 18:00
 **/
public class Ad2Test {

	private List<DC5Adapter> adapters = new LinkedList<>();

	public Ad2Test() {
		this.adapters.add(new ChinaPowerAdapter());
		this.adapters.add(new JapanPowerAdapter());
	}

	// 根据电压找合适的变压器
	public DC5Adapter getPowerAdapter(AC ac) {
		DC5Adapter adapter = null;
		for (DC5Adapter ad : this.adapters) {
			if (ad.support(ac)) {
				adapter = ad;
				break;
			}
		}
		if (adapter == null){
			throw new  IllegalArgumentException("没有找到合适的变压适配器");
		}
		return adapter;
	}

	public static void main(String[] args) {
		Ad2Test test = new Ad2Test();

		// 中国
		AC chinaAC = new AC220();
		DC5Adapter adapter = test.getPowerAdapter(chinaAC);
		adapter.outputDC5V(chinaAC);

		// 日本
		AC japanAC = new AC110();
		adapter = test.getPowerAdapter(japanAC);
		adapter.outputDC5V(japanAC);
	}
}
